// pages/accountRecharge/accountRecharge.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceList: [],
    state: '',
    payable: 0,
    userInfo: [],
    recharge: "",
    inputRecharge: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

    this.getMoneyList();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo();
  },


  //获取需要支付的金额
  getNeedPayMoney(chooseMoney) {
    app.ajax({
      url: "Partner/Users/getPayMoney",
      data: {
        money: chooseMoney
      },
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            payable: res.data.data.pay_money
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("获取应付金额失败")
      }
    })
  },



  // 选择充值金额
  choosePrice: function (e) {
    const {
      in: state, price
    } = e.currentTarget.dataset;
    this.getNeedPayMoney(price);
    this.setData({
      state,
      recharge: price
    })
  },

  // 其他金额
  otherPrice: function (e) {
    var that = this;
    that.setData({
      state: 100,
    })
  },

  price: function (e) {
    let value = e.detail.value;
    this.setData({
      inputRecharge: app.retainDecimal(value)
    })
  },

  otherPriceBlur(e) {
    this.getNeedPayMoney(e.detail.value)
  },

  //账户余额
  getUserInfo() {
    var that = this;
    app.ajax({
      url: 'Partner/User/getPartnerInfo',
      data: {},
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            userInfo: res.data.data,
          })
        }
      },
    })
  },
  //充值金额列表
  getMoneyList() {
    var that = this;
    app.ajax({
      url: "Partner/Users/money_conf",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            priceList: res.data.data
          })
        }
      }
    })
  },

  //支付
  pay() {
    var that = this;
    var money = that.data.recharge;
    if (that.data.state == 100) {
      money = that.data.inputRecharge
    }
    if (money === "") {
      app.showToast("请选择充值金额");
      return false;
    }
    app.ajax({
      url: "Partner/Users/wxPay",
      data: {
        "money": money
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (that.data.payable == 0) {
            app.showToast("支付成功", "success");
            setTimeout(() => {
              wx.navigateBack();
            }, 1500)
          } else {
            var timeStamp = res.data.data.timeStamp;
            var nonceStr = res.data.data.nonceStr;
            var packages = res.data.data.package;
            var signType = res.data.data.sign_type;
            var paySign = res.data.data.paySign;
            wx.requestPayment({
              'timeStamp': timeStamp,
              'nonceStr': nonceStr,
              'package': packages,
              'signType': 'MD5',
              'paySign': paySign,
              complete: function (res) {
                console.log(res);
                if (res.errMsg == 'requestPayment:ok') {
                  // that.getUserInfo();
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1000);
                }
              }
            })
          }
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  }
})